package com.sansan.qiangji.realize.device;

import com.sansan.qiangji.VO.device.BeltParamDetailVO;
import com.sansan.qiangji.VO.device.BeltVO;
import com.sansan.qiangji.entity.device.Belt;
import com.sansan.qiangji.entity.device.BeltParamDetail;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.device.belt.BeltAddForm;
import com.sansan.qiangji.form.device.belt.BeltEditForm;
import com.sansan.qiangji.service.impl.device.BeltParamDetailServiceImpl;
import com.sansan.qiangji.service.impl.device.BeltServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BeltReal {
    private BeltServiceImpl beltService;
    private BeltParamDetailServiceImpl beltParamDetailService;

    @Resource
    public void setBeltService(BeltServiceImpl beltService){this.beltService = beltService;}

    @Resource
    public void setBeltParamDetailService(BeltParamDetailServiceImpl beltParamDetailService){
        this.beltParamDetailService = beltParamDetailService;
    }

    public List<BeltVO> beltList(){
        List<BeltVO> beltVOS = new ArrayList<>();
        List<Belt> belts = beltService.findAllBelt();
        for (Belt belt : belts){
            BeltVO beltVO = new BeltVO();
            BeanUtils.copyProperties(belt, beltVO);
            beltVOS.add(beltVO);
        }
        return beltVOS;
    }

    @Transactional
    public void beltCreate(BeltAddForm beltAddForm) {
        List<Belt> belts = beltService.findByBeltName(beltAddForm.getBeltName());
        if (belts.size()!=0){
            log.error("【添加传送带】传送带已存在");
            throw new ServiceException(ResultEnum.BELT_IS_EXIST);
        }
        Belt belt = new Belt();
        BeanUtils.copyProperties(beltAddForm, belt);
        belt.setBeltOnlineStatus(false);
        beltService.addBelt(belt);
    }

    @Transactional
    public void beltEdit(BeltEditForm beltEditForm){
        Belt belt = beltService.findByBeltId(beltEditForm.getBeltId());
        if (belt == null){
            log.error("【编辑传送带】传送带不存在");
            throw new ServiceException(ResultEnum.BELT_IS_NOT_EXIST);
        }
        BeanUtils.copyProperties(beltEditForm, belt);
        beltService.updateBelt(belt);
    }

    @Transactional
    public void deleteBelt(Integer beltId){
        Belt belt = beltService.findByBeltId(beltId);
        if (belt == null){
            log.error("【删除传送带】传送带不存在");
            throw new ServiceException(ResultEnum.BELT_IS_NOT_EXIST);
        }
        beltService.deleteById(beltId);
    }

    @Transactional
    public List<BeltParamDetailVO> getBeltParamDetail(Integer beltId) {
        List<BeltParamDetailVO> beltParamDetailVOS = new ArrayList<>();
        Belt belt = beltService.findByBeltId(beltId);
        if (belt == null){
            log.error("【传送带参数】传送带不存在");
            throw new ServiceException(ResultEnum.BELT_IS_NOT_EXIST);
        }
        belt.setIsRefreshBeltParam(true);
        beltService.updateBelt(belt);
        List<BeltParamDetail> beltParamDetails = beltParamDetailService.findByBeltName(belt.getBeltName());
        for (BeltParamDetail beltParamDetail : beltParamDetails){
            BeltParamDetailVO beltParamDetailVO = new BeltParamDetailVO();
            BeanUtils.copyProperties(beltParamDetail, beltParamDetailVO);
            beltParamDetailVOS.add(beltParamDetailVO);
        }
        return beltParamDetailVOS;
    }
}
